/* eslint-disable react/prop-types */
import React, { useReducer } from 'react'
//7个组件（在结果中显示，越高越好/越低越好，总分划界分（满分：100），上面的3个配置，解读，因子，总的）
const list = [
  {
    factorid: 2718974317,
    name: '因子1',
    show: 1,
    evaluation_criteria: 1,
    demarcation_score: 60,
    analysis: [{ code: 1212, min: 20, max: 30, content: '和欧俄劈腿业务1' },
      { code: 12121, min: 30, max: 40, content: '和欧俄劈腿业务2' }]
  },
  {
    factorid: 271897431789,
    name: '因子2',
    show: 0,
    evaluation_criteria: 0,
    demarcation_score: 60,
    analysis: [{ code: 122, min: 20, max: 30, content: '和欧俄1' },
      { code: 121, min: 30, max: 40, content: 'ewr务2' }]
  }
]

function listReducer (state, action) {
  const { name, value, index, i } = action.payload
  let list = [...state.list]
  switch (action.type) {
  case 'factor':
    list[index][name] = value
    return { list }
  case 'analysis':
    list[index].analysis[i][name] = value
    return { list }
  case 'addAnalysis':
    list[index].analysis.push({ code: Math.random() * 1000, min: 0, max: 0, content: '' })
    return { list }
  default:
    throw new Error()
  }
}

// 在结果中显示
function Show (props) {
  const [state, dispatch] = useReducer(listReducer, { list })
  const show = state.list[props.index].show

  // 因子input改变的时候
  const factorDispatch = (e, type) => {
    let payload = {
      name: e.target.name,
      value: e.target.value,
      index: props.index
    }
    dispatch({ type, payload })
  }

  return (
    <li>
      <input type='checkbox' name='show' value={show} onChange={(e) => factorDispatch(e, 'factor')} />在结果中显示
    </li>
  )
}

// 越高越好/越低越好
function EvaluationCriteria (props) {
  const [state, dispatch] = useReducer(listReducer, { list })
  const evaluation_criteria = state.list[props.index].evaluation_criteria

  // 因子input改变的时候
  const factorDispatch = (e, type) => {
    let payload = {
      name: e.target.name,
      value: e.target.value,
      index: props.index
    }
    dispatch({ type, payload })
  }

  return (<>
    <li>
      <input type='radio' name='evaluation_criteria' value={evaluation_criteria} onChange={(e) => factorDispatch(e, 'factor')} />越高越好
      <input type='radio' name='evaluation_criteria' value={evaluation_criteria} onChange={(e) => factorDispatch(e, 'factor')} />越低越好
    </li>
  </>)
}

// 总分划界分（满分：100）
function DemarcationScore (props) {
  const [state, dispatch] = useReducer(listReducer, { list })
  const demarcation_score = state.list[props.index].demarcation_score

  // 因子input改变的时候
  const factorDispatch = (e, type) => {
    let payload = {
      name: e.target.name,
      value: e.target.value,
      index: props.index
    }
    dispatch({ type, payload })
  }

  return (<>
    <li>
      <p>总分划界分（满分：100）</p>
      <input type='input' name='demarcation_score' value={demarcation_score} onChange={(e) => factorDispatch(e, 'factor')} />
    </li>
  </>)
}

// 上面的3个配置
function Configure (props) {
  return (
    <>
      <Show index={props.index} />
      <EvaluationCriteria index={props.index} />
      <DemarcationScore index={props.index} />
    </>
  )
}

// 解读
function Analysis (props) {
  const [state, dispatch] = useReducer(listReducer, { list })
  const analysisList = state.list[props.index].analysis

  // 解读input改变的时候
  const analysisDispatch = (e, type, i) => {
    let payload = {
      name: e.target.name,
      value: e.target.value,
      index: props.index,
      i
    }
    dispatch({ type, payload })
  }

  // 新增一条解读
  const addAnalysisDispatch = (type) => {
    let payload = {
      index: props.index
    }
    dispatch({ type, payload })
  }

  return (<>
    <li>解读：</li>
    {
      analysisList.map((val, i) => {
        return <li key={val.code}>
          <p>当（得分-划界分）在</p>
          <input type='input' name='min' value={val.min} onChange={(e) => analysisDispatch(e, 'analysis', i)} />～
          <input type='input' name='max' value={val.max} onChange={(e) => analysisDispatch(e, 'analysis', i)} />
          <p>之间，则显示</p>
          <textarea rows="3" cols="20" name='content' value={val.content} onChange={(e) => analysisDispatch(e, 'analysis', i)} />
        </li>
      })
    }
    <button onClick={() => addAnalysisDispatch('addAnalysis')}>新增一条解读</button>
  </>)
}

// 因子
function Factor (props) {
  const [state] = useReducer(listReducer, { list })
  const factor = state.list[props.index]

  return (
    <ul>
      <h2>{factor.name}</h2>
      <Configure index={props.index} />
      <Analysis index={props.index} />
    </ul>
  )
}

// 总的
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function Plan2 () {
  const [state] = useReducer(listReducer, { list })
  // 提交
  const submit = () => {
    console.log('submit', state.list)
  }

  return (
    <div>
      {
        state.list.map((item, index) => {
          return <Factor key={item.factorid} index={index} />
        })
      }
      <button onClick={submit} >提交</button>
    </div>
  )
}

export default Plan2